<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<style>
    .layui-table-cell {
        height: inherit;
    }
</style>
<body>
<div class="layui-fluid">
    <fieldset class="layui-elem-field">
        <legend>用户管理</legend>
        <div class="layui-field-box">
            <div class="layui-card">
                <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户信息</label>
                            <div class="layui-input-inline">
                                <input type="text" name="info" placeholder="请输入用户昵称或手机号" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-contlist-search">
                                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                            </button>
                        </div>
                    </div>
                </div>
                <div class="layui-card-body higo-card">
                    <table id="demo" lay-filter="demo"></table>
                    <script type="text/html" id="name">
                        <div style="display: flex">
                            <img class="imgs" src="{{d.portrait}}" title="{{d.name}}" style="width: 50px;height: 50px;cursor: pointer;">
                            <div style="margin-left: 15px;">
                                <div style="flex-wrap: wrap;">{{d.name}} {{# if(d.super){ }}<span style="color: red;border: 1px solid red;font-size: 13px;padding: 2px;margin: 2px;">超</span>{{# } }}{{# if(d.retail){ }}<span style="color: red;border: 1px solid red;font-size: 13px;padding: 2px;margin: 2px;">销</span>{{# } }}{{# if(d.pf){ }}<span style="color: red;border: 1px solid red;font-size: 13px;padding: 2px;margin: 2px;">批</span>{{# } }}</div>
                                <div style="flex-wrap: wrap;">{{d.phone}}</div>
                            </div>
                        </div>
                    </script>
                    <script type="text/html" id="yue">
                        <div>余额: {{ returnFloat(d.balance)}} ¥</div>
                        <div>赠送余额: {{ returnFloat(d.give)}} ¥</div>
                        <div>返现余额: {{ returnFloat(d.cashback)}} ¥</div>
                        <div>佣金余额: {{ returnFloat(d.gold)}} ¥</div>
                    </script>
                    <script type="text/html" id="time">
                        {{ formatDateTime(d.time) }}
                    </script>
                    <script type="text/html" id="table-content-list">
                        <a class="layui-btn layui-btn-normal layui-btn-xs operation">操作<i class="layui-icon layui-icon-triangle-d"></i></a>
                        <ul class="layui-nav-child layui-anim layui-anim-upbit" style="padding: 10px; top: 22px; min-width: inherit; position: absolute; display: none;">
                            <li>
                                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit" data-id="{{d.id}}">编辑</a>
                            </li>
                            <li>
                                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="look" data-id="{{d.id}}">查看下级</a>
                            </li>
                            <li>
                                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="jilu" data-id="{{d.id}}">消费记录</a>
                            </li>
                            <li>
                                {{# if (d.super == 0){ }}
                                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="super" data-id="{{d.id}}">设为超管</a>
                                {{# }else{ }}
                                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="super2" data-id="{{d.id}}">取消超管</a>
                                {{# } }}
                            </li>
                            <li>
                                {{# if (d.pf == 0){ }}
                                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="pifa" data-id="{{d.id}}">设为批发会员</a>
                                {{# }else{ }}
                                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="pifa2" data-id="{{d.id}}">取消批发会员</a>
                                {{# } }}
                            </li>
                            <li>
                                {{# if (d.retail == 0){ }}
                                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="retail" data-id="{{d.id}}">开启分销</a>
                                {{# }else{ }}
                                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="retail2" data-id="{{d.id}}">关闭分销</a>
                                {{# } }}
                            </li>
                            <li>
                                {{# if (d.is_jin == 0){ }}
                                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="jin" data-id="{{d.id}}">禁止登录</a>
                                {{# }else{ }}
                                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="jin2" data-id="{{d.id}}">允许登录</a>
                                {{# } }}
                            </li>
                            <li>
                                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="grade" data-id="{{d.id}}">会员等级</a>
                            </li>
                        </ul>
                    </script>
                </div>
            </div>
        </div>
    </fieldset>
</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'useradmin', 'table'], function(){
        var $ = layui.$,
                form = layui.form,
                table = layui.table;

        //监听搜索
        form.on('submit(LAY-app-contlist-search)', function(data){
            var field = data.field;
            //执行重载
            table.reload('demo', {
                url: "{:url('User/index')}",
                where: field,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });

        //执行渲染
        table.render({
            elem: '#demo',
            url: "{:url('User/index')}",
            method: 'post',
            cols: [[
                {field: 'name', title: '会员昵称', templet: "#name", width: 220},
                {field: 'grade_name', title: '会员等级'},
                {field: 'yue', title: '余额', templet: "#yue"},
//                {field: 'openid', title: '微信openid'},
                {field: 'upper', title: '所属上级'},
                {field: 'time', title: '加入时间', width: 160, templet: "#time"},
                {fixed: 'right', title: '操作', width: 120, align:'center', toolbar: '#table-content-list'}
            ]],
            page: {
                limits: [10, 20, 30, 40, 50]
            }
        });

        //监听事件
        table.on('tool(demo)', function(obj){
            var id = obj.data.id;
            var layEvent = obj.event;
            if(layEvent === 'look'){ //编辑
                layer.open({
                    type: 2
                    ,title: '下级会员'
                    ,content: '/admin/User/lower/id/' + id
                    ,maxmin: true
                    ,area: ['100%', '100%']
                    ,btn: '关闭'
                    ,btnAlign: 'c'
                    ,yes: function(index, layero){
                        layer.close(index);
                    }
                });
            } else if (layEvent === 'super'){
                layer.confirm('确定设置当前用户为超级管理员吗?', {
                    icon: 3,
                    title:'提示'
                }, function(index){
                    //do something
                    $.post("{:url('User/super')}", {id:id,type:1}, function(e){
                        if (e == 1){
                            layer.msg('操作成功');
                            setTimeout(function (){
                                table.reload('demo');
                            }, 500);
                        }else{
                            layer.msg('操作失败');
                        }
                    })
                });
            } else if (layEvent === 'super2'){
                layer.confirm('确定取消当前用户超级管理员吗?', {
                    icon: 3,
                    title:'提示'
                }, function(index){
                    //do something
                    $.post("{:url('User/super')}", {id:id,type:2}, function(e){
                        if (e == 1){
                            layer.msg('操作成功');
                            setTimeout(function (){
                                table.reload('demo');
                            }, 500);
                        }else{
                            layer.msg('操作失败');
                        }
                    })
                });
            } else if (layEvent === 'pifa'){
                layer.confirm('确定设置当前用户为批发会员吗?', {
                    icon: 3,
                    title:'提示'
                }, function(index){
                    //do something
                    $.post("{:url('User/pf')}", {id:id,type:1}, function(e){
                        if (e == 1){
                            layer.msg('操作成功');
                            setTimeout(function (){
                                table.reload('demo');
                            }, 500);
                        }else{
                            layer.msg('操作失败');
                        }
                    })
                });
            } else if (layEvent === 'pifa2'){
                layer.confirm('确定取消当前用户批发会员吗?', {
                    icon: 3,
                    title:'提示'
                }, function(index){
                    //do something
                    $.post("{:url('User/pf')}", {id:id,type:2}, function(e){
                        if (e == 1){
                            layer.msg('操作成功');
                            setTimeout(function (){
                                table.reload('demo');
                            }, 500);
                        }else{
                            layer.msg('操作失败');
                        }
                    })
                });
            } else if (layEvent === 'retail'){
                layer.confirm('确定开启分销吗?', {
                    icon: 3,
                    title:'提示'
                }, function(index){
                    //do something
                    $.post("{:url('User/retail')}", {id:id,type:1}, function(e){
                        if (e == 1){
                            layer.msg('操作成功');
                            setTimeout(function (){
                                table.reload('demo');
                            }, 500);
                        }else{
                            layer.msg('操作失败');
                        }
                    })
                });
            } else if (layEvent === 'retail2'){
                layer.confirm('确定关闭分销吗?', {
                    icon: 3,
                    title:'提示'
                }, function(index){
                    //do something
                    $.post("{:url('User/retail')}", {id:id,type:2}, function(e){
                        if (e == 1){
                            layer.msg('操作成功');
                            setTimeout(function (){
                                table.reload('demo');
                            }, 500);
                        }else{
                            layer.msg('操作失败');
                        }
                    })
                });
            } else if (layEvent === 'jin'){
                layer.confirm('确定禁止当前用户登录吗?', {
                    icon: 3,
                    title:'提示'
                }, function(index){
                    //do something
                    $.post("{:url('User/jin')}", {id:id,type:1}, function(e){
                        if (e == 1){
                            layer.msg('操作成功');
                            setTimeout(function (){
                                table.reload('demo');
                            }, 500);
                        }else{
                            layer.msg('操作失败');
                        }
                    })
                });
            } else if (layEvent === 'jin2'){
                layer.confirm('确定取消禁止当前用户登录吗?', {
                    icon: 3,
                    title:'提示'
                }, function(index){
                    //do something
                    $.post("{:url('User/jin')}", {id:id,type:2}, function(e){
                        if (e == 1){
                            layer.msg('操作成功');
                            setTimeout(function (){
                                table.reload('demo');
                            }, 500);
                        }else{
                            layer.msg('操作失败');
                        }
                    })
                });
            } else if (layEvent === 'edit'){
                layer.open({
                    type: 2
                    ,title: '编辑用户信息'
                    ,content: '/admin/User/user_operate/id/' + id
                    ,area: ['500px', '400px']
                    ,btn: ['确定', '取消']
                    ,btnAlign: 'c'
                    ,yes: function(index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");
                        submit.click();
                    }
                });
            } else if (layEvent === 'jilu'){
                layer.open({
                    type: 2
                    ,title: '消费记录'
                    ,content: '/admin/User/user_jilu/id/' + id
                    ,area: ['100%', '100%']
                    ,btn: '关闭'
                    ,btnAlign: 'c'
                });
            } else if (layEvent === 'grade'){
                layer.open({
                    type: 2
                    ,title: '会员等级'
                    ,content: '/admin/User/user_grade_operate/id/' + id
                    ,area: ['400px', '300px']
                    ,btn: ['修改', '取消']
                    ,btnAlign: 'c'
                    ,yes: function(index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");
                        submit.click();
                    }
                });
            }
        });
    });

    //php时间戳用js转换为yyyy-mm-dd
    function formatDateTime(timeStamp) {
        var date = new Date();
        date.setTime(timeStamp * 1000);
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        var second = date.getSeconds();
        minute = minute < 10 ? ('0' + minute) : minute;
        second = second < 10 ? ('0' + second) : second;
        return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
    }

    //补零
    function returnFloat(value){
        value = Math.round(parseFloat(value)*100)/100;
        var xsd = value.toString().split(".");
        if(xsd.length == 1){
            value = value.toString()+".00";
            return value;
        }
        if(xsd.length>1){
            if(xsd[1].length<2){
                value = value.toString()+"0";
            }
            return value;
        }
    }
</script>
</body>
</html>
